﻿//
// Created by work on 24-6-6.
//

#include "CRtModelParameterInfo.h"


std::map<CRtModelParameterInfo::EModelParameterCol, QString> CRtModelParameterInfo::rtModelParameterColToStringMap = {
    {CRtModelParameterInfo::EPtype, "ptype"},
    {CRtModelParameterInfo::EEn_write, "en_write"},
    {CRtModelParameterInfo::EEn_read, "en_read"}
};

std::map<CRtModelParameterInfo::EModelParameterCol, QString> CRtModelParameterInfo::parameterColFieldsNameMap = {
    {CRtModelParameterInfo::EModelParameterCol::EPtype, "参数类型"},
    {CRtModelParameterInfo::EModelParameterCol::EEn_write, "是否允许写入"},
    {CRtModelParameterInfo::EModelParameterCol::EEn_read, "是否允许读取"}
};

std::map<CRtModelParameterInfo::EModelParameterCol, CValue::EValueType> CRtModelParameterInfo::rtParameterDataTypeByField = {
    {CRtModelParameterInfo::EPtype, CValue::IOT_Int},
    {CRtModelParameterInfo::EEn_write, CValue::IOT_Bool},
    {CRtModelParameterInfo::EEn_read, CValue::IOT_Bool}
};


CRtModelParameterInfo::CRtModelParameterInfo(QObject *parent) : CRtModelSigBase(PARAMETER_COL_SIZE, parent){
    QMap<int, QString> vmap;
    vmap.insert(0, "否");
    vmap.insert(1, "是");
    m_fieldValueMap.insert(parameterColFieldName(PARAMETER_ENUM::EEn_read), vmap);
    m_fieldValueMap.insert(parameterColFieldName(PARAMETER_ENUM::EEn_write), vmap);

    QMap<int, QString> parmap;
    parmap.insert(0, "Int");
    parmap.insert(1, "String");
    parmap.insert(2, "Float");
    m_fieldValueMap.insert(parameterColFieldName(PARAMETER_ENUM::EPtype), parmap);
}

QString CRtModelParameterInfo::getFieldNameByCol(int col)
{
    if(col < 0 || col > (PARAMETER_COL_SIZE-1))
        return "";

    if(col < SIGBASE_COL_SIZE)
        return CRtModelSigBase::getFieldNameByCol(col);

    return parameterColFieldName(static_cast<EModelParameterCol>(col));
}

QVariant CRtModelParameterInfo::getFieldValueByFielddName(const QString &name)
{
    if(name.isEmpty())
        return QVariant();

    if(parameterColFieldName(PARAMETER_ENUM::EPtype) == name) {
        return static_cast<int>(ptype);
    } else if(parameterColFieldName(PARAMETER_ENUM::EEn_read) == name) {
        return en_read ? 1 : 0;
    } else if(parameterColFieldName(PARAMETER_ENUM::EEn_write) == name) {
        return en_write ? 1 : 0;
    } else {
        return CRtModelSigBase::getFieldValueByFielddName(name);
    }
}

void CRtModelParameterInfo::setFieldValueByCol(int col, const QVariant &value)
{

}

void CRtModelParameterInfo::fromJson(const QJsonObject& json) {
    CRtModelSigBase::fromJson(json);
    ptype = static_cast<EParameterType>(json[parameterFieldName(PARAMETER_ENUM::EPtype)].toInt());
    en_write = json[parameterFieldName(PARAMETER_ENUM::EEn_write)].toBool();
    en_read = json[parameterFieldName(PARAMETER_ENUM::EEn_read)].toBool();
}

QJsonObject CRtModelParameterInfo::toJson() {
    QJsonObject json = CRtModelSigBase::toJson();
    json[parameterFieldName(PARAMETER_ENUM::EPtype)] = static_cast<int>(ptype);
    json[parameterFieldName(PARAMETER_ENUM::EEn_write)] = en_write;
    json[parameterFieldName(PARAMETER_ENUM::EEn_read)] = en_read;
    return json;
}

QSharedPointer<IModel> CRtModelParameterInfo::createEmpty()
{

}

bool CRtModelParameterInfo::sync(bool dir)
{

}
